package resources.service;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import resources.dao.RoleDAO;
import resources.dao.UserDAO;

import resources.domain.Role;
import resources.domain.User;

/**
 * Spring service that handles CRUD requests for Role entities
 * 
 */

@Service("RoleService")
@Transactional
public class RoleServiceImpl implements RoleService {

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * DAO injected by Spring that manages User entities
	 * 
	 */
	@Autowired
	private UserDAO userDAO;

	/**
	 * Instantiates a new RoleServiceImpl.
	 *
	 */
	public RoleServiceImpl() {
	}

	/**
	 * Delete an existing Role entity
	 * 
	 */
	@Transactional
	public void deleteRole(Role role) {
		roleDAO.remove(role);
		roleDAO.flush();
	}

	/**
	 * Return a count of all Role entity
	 * 
	 */
	@Transactional
	public Integer countRoles() {
		return ((Long) roleDAO.createQuerySingleResult("select count(o) from Role o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing User entity
	 * 
	 */
	@Transactional
	public Role saveRoleUsers(Integer id, User related_users) {
		Role role = roleDAO.findRoleByPrimaryKey(id, -1, -1);
		User existingusers = userDAO.findUserByPrimaryKey(related_users.getId());

		// copy into the existing record to preserve existing relationships
		if (existingusers != null) {
			existingusers.setId(related_users.getId());
			existingusers.setName(related_users.getName());
			related_users = existingusers;
		} else {
			related_users = userDAO.store(related_users);
			userDAO.flush();
		}

		related_users.setRole(role);
		role.getUsers().add(related_users);
		related_users = userDAO.store(related_users);
		userDAO.flush();

		role = roleDAO.store(role);
		roleDAO.flush();

		return role;
	}

	/**
	 * Return all Role entity
	 * 
	 */
	@Transactional
	public List<Role> findAllRoles(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Role>(roleDAO.findAllRoles(startResult, maxRows));
	}

	/**
	 * Save an existing Role entity
	 * 
	 */
	@Transactional
	public void saveRole(Role role) {
		Role existingRole = roleDAO.findRoleByPrimaryKey(role.getId());

		if (existingRole != null) {
			if (existingRole != role) {
				existingRole.setId(role.getId());
				existingRole.setName(role.getName());
			}
			role = roleDAO.store(existingRole);
		} else {
			role = roleDAO.store(role);
		}
		roleDAO.flush();
	}

	/**
	 * Load an existing Role entity
	 * 
	 */
	@Transactional
	public Set<Role> loadRoles() {
		return roleDAO.findAllRoles();
	}

	/**
	 */
	@Transactional
	public Role findRoleByPrimaryKey(Integer id) {
		return roleDAO.findRoleByPrimaryKey(id);
	}

	/**
	 * Delete an existing User entity
	 * 
	 */
	@Transactional
	public Role deleteRoleUsers(Integer role_id, Integer related_users_id) {
		User related_users = userDAO.findUserByPrimaryKey(related_users_id, -1, -1);

		Role role = roleDAO.findRoleByPrimaryKey(role_id, -1, -1);

		related_users.setRole(null);
		role.getUsers().remove(related_users);

		userDAO.remove(related_users);
		userDAO.flush();

		return role;
	}
}
